package com.iqiyi.game.ams.infrastructure.app;

import com.iqiyi.game.ams.ui.dto.ResponseDTO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author hanbaoyu
 */

@ControllerAdvice
public class AppExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseDTO handle(Exception e) {

        if (e instanceof AppException) {
            AppException appException = (AppException) e;
            return ResponseDTO.error(appException.getCode(), appException.getMessage());
        }
        // 需要报警的日志

        // 记日志
        e.printStackTrace();
        return ResponseDTO.error(ResponseEnum.UNKNOWN_ERROR.getCode(), e.toString() + " " + e.getMessage());
    }
}
